package com.mingtu.guards.fragment;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.mingtu.common.base.BaseFragment;
import com.mingtu.common.callback.StringDialogCallback;
import com.hjq.gson.factory.GsonFactory;
import com.mingtu.common.utils.GridSpacingItemDecoration;
import com.mingtu.common.utils.ScreenUtils;
import com.mingtu.common.view.EmptyDataLayout;
import com.mingtu.common.viewpager.LazyViewPager.LazyFragmentPagerAdapter;
import com.mingtu.guards.R;
import com.mingtu.guards.adapter.EcHydroAdapter;
import com.mingtu.guards.bean.HydroDataBean;
import com.mingtu.guards.utils.MyConstant;

import java.util.HashMap;

import butterknife.BindView;

public class EcHydroFragment extends BaseFragment implements LazyFragmentPagerAdapter.Laziable {
    @BindView(R.id.recycler)
    RecyclerView recycler;
    private EmptyDataLayout emptyLayout;
    private EcHydroAdapter ecHydroAdapter;
    private String devId;
    private static final String ARG_PARAM1 = "param1";

    public static EcHydroFragment getInstance(String devId) {
        EcHydroFragment ef = new EcHydroFragment();
        Bundle bundle = new Bundle();
        bundle.putString(ARG_PARAM1, devId);
        ef.setArguments(bundle);
        return ef;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            devId = getArguments().getString(ARG_PARAM1);
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_ec_hydro;
    }

    @Override
    protected void initData() {
        int spanCount = ScreenUtils.isTablet() ? 3 : 2;
        ecHydroAdapter = new EcHydroAdapter(context, null);
        GridLayoutManager layoutManager = new GridLayoutManager(context, spanCount);//第二个参数为网格的列数
        recycler.addItemDecoration(new GridSpacingItemDecoration(spanCount, SizeUtils.dp2px(5), false));
//        recycler.setHasFixedSize(true);
        recycler.setLayoutManager(layoutManager);
        recycler.setAdapter(ecHydroAdapter);


    }

    @Override
    protected void initView(View view) {
        emptyLayout = (EmptyDataLayout) view.findViewById(R.id.empty_layout);
    }

    @Override
    protected void getData() {
        getHydroData();
    }

    /**
     * 刷新数据
     */
    public void refresh() {
        getHydroData();
    }

    /**
     * 水文数据
     */
    private void getHydroData() {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("devId", devId);
        String str = GsonUtils.toJson(hashMap);
        OkGo.<String>post(MyConstant.POST_HYDRO_DATA)
                .tag(this)
                .upJson(str)
                .execute(new StringDialogCallback(getActivity()) {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String result = response.body();
                        if (!StringUtils.isEmpty(result)) {

                            HydroDataBean hydroDataBean = null;
                            try {
                                Gson gson = GsonFactory.getSingletonGson();
                                hydroDataBean = new HydroDataBean();
                                hydroDataBean = gson.fromJson(result, HydroDataBean.class);
                                HydroDataBean.DataBean dataBean = hydroDataBean.getData();

                                try {
                                    emptyLayout.setIsShowView(false);
                                    recycler.setVisibility(View.VISIBLE);

                                    if (dataBean != null) {
                                        ecHydroAdapter.upData(hydroDataBean);
                                    } else {
                                        //显示占位图
                                        recycler.setVisibility(View.GONE);
                                        emptyLayout.setIsShowView(true);

                                    }
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }

                            } catch (JsonSyntaxException e) {
                                e.printStackTrace();
                                emptyLayout.setIsShowView(true);
                                recycler.setVisibility(View.GONE);
                                ToastUtils.showLong(getResources().getString(R.string.text_json_error));

                            }


                        }
                    }
                });
    }


}